package models;

import Dao.DBConnect;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class UserModel {

    int id;
    String uname;
    String passwd;



    public UserModel() {
    }

    int role;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public int getRole() {
        return role;
    }

    public String hashit(String plaintext) throws NoSuchAlgorithmException {

        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(plaintext.getBytes());
        byte[] digest = m.digest();
        BigInteger bigInt = new BigInteger(1,digest);
        String hashtext = bigInt.toString(16);
// Now we need to zero pad it if you actually want the full 32 chars.
        while(hashtext.length() < 32 ){
            hashtext = "0"+hashtext;
        }
        return  hashtext;

    }

    @Override
    public String toString() {
        return "UserModel{" +
                "id=" + id +
                ", uname='" + uname + '\'' +
                ", passwd='" + passwd + '\'' +
                ", role=" + role +
                '}';
    }

    public void setRole(int role) {
        this.role = role;
    }

    DBConnect conn = null;
    Statement stmt = null;



    public List<UserModel> getAll() {
        List<UserModel> ums = new ArrayList<>();
        String query = "select * from mian_wang_users  ;";
        try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(query)) {
//            statement.setInt(1, cid);

            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                UserModel um = new UserModel();
                // grab record data by table field name into ClientModel account object
                um.setId(resultSet.getInt("id"));
                um.setUname(resultSet.getString("uname"));
                um.setPasswd(resultSet.getString("passwd"));
                um.setRole((resultSet.getInt("role")));
                ums.add(um); // add account data to arraylist
            }

        } catch (SQLException e) {
            System.out.println("Error fetching Accounts: " + e);
        }
        System.out.println(ums.toString());
        return ums; // return arraylist
    }

    public void resetPass(UserModel um) {
        try {
            System.out.println("Update record in user table...");
//            stmt = conn.getConnection().createStatement();
            String sql = null;
//            String sql2 = null;

            // Include data to the database table

            sql = " update  mian_wang_users   set  passwd = ?  where id = ?;\n";
            System.out.println(sql);
            try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(sql)) {
//            statement.setInt(1, cid);
//                statement.setString(1,um.getUname());
                statement.setString(1,hashit(um.getPasswd()));
//                statement.setInt(3, um.getRole());
                statement.setInt(2, um.getId());
                System.out.println(statement.toString());
                statement.executeUpdate();
            }


        } catch (SQLException | NoSuchAlgorithmException se) {
            se.printStackTrace();
        }

    }

    public void insertRecord(UserModel um) {

        try {
//			setCid(cid);
            // Execute a query
            System.out.println("Inserting record into the user table...");
            stmt = DBConnect.getConnection().createStatement();
            String sql = null;

            sql = " insert into mian_wang_users(uname, passwd,role) values('" + um.getUname()  + "', '" + hashit(um.getPasswd()) + "','" + um.getRole() +"')";

            stmt.executeUpdate(sql);

        } catch (SQLException | NoSuchAlgorithmException se) {
            se.printStackTrace();
        }
    }

    public Boolean checkuser(UserModel um2) {

//			setCid(cid);
        // Execute a query
        String sql = null;
        Boolean res = Boolean.FALSE;
        int count=0;
        sql = " select count(*) from  mian_wang_users where uname = ? ";
        System.out.println("check record into the user table...");
        try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(sql)) {
            statement.setString(1, um2.getUname());
            ResultSet resultSet = statement.executeQuery();
            System.out.println(statement.toString());
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
            if (count > 0) {
                 res = Boolean.TRUE;
            } 
            else 
                {

                res = Boolean.FALSE;
            }



        } catch (SQLException e) 
        {
            e.printStackTrace();
        }
        return res;    }

}
